<template>
  <div class="book-detail">
    <t-card :bordered="false" class="content-card">
      <template #title>
        <t-space size="24px">
          <h2 class="card-title">产品库</h2>
        </t-space>
      </template>
      <template #actions>
        <t-space size="8px">
          <t-button theme="default" class="action-btn action-btn1" @click="goProductAdd">
            <icon-font name="add" size="12px"/>
            新增产品
          </t-button>
        </t-space>
      </template>

      <div class="members">
        <div class="member-list">
          <div class="member-item" v-for="item in tabList" :key="item.value" @click="changeTab(item)">
            <div class="body-wrap" :class="{'active-member': item.value === params.status}">
              <p class="num">{{ item.num }}</p>
              <div class="body">
                <h3>{{ item.label }}</h3>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="filter ">
        <t-space size="12px" class="flex-5">
          <!--<t-date-picker style="width: 140px;" v-model="params.date" :prefix-icon="renderPrefixIcon" enable-time-picker allow-input clearable  format="YYYY-MM-DD HH:mm:ss">-->
          <!--  <template #suffixIcon>-->
          <!--    <icon-font name="chevron-down"/>-->
          <!--  </template>-->
          <!--</t-date-picker>-->
          <t-select style="width: 140px;" v-model="params.classify_id" :options="classifyOptions"
                    placeholder="按产品分类">
            <template #label>
              <icon-font name="layers" style="color: #000;"/>
            </template>
          </t-select>
          <t-select style="width: 140px;" v-model="params.brand_id" :options="brandOptions" placeholder="按产品品牌">
            <template #label>
              <icon-font name="layers" style="color: #000;"/>
            </template>
          </t-select>
          <t-select style="width: 140px;" v-model="params.brand_id" :options="brandOptions" placeholder="按产品机型">
            <template #label>
              <icon-font name="layers" style="color: #000;"/>
            </template>
          </t-select>
          <!--<t-select style="width: 140px;" v-model="params.hot_level" :options="levelOptions" placeholder="按推荐等级">-->
          <!--  <template #label>-->
          <!--    <icon-font name="layers" style="color: #000;"/>-->
          <!--  </template>-->
          <!--</t-select>-->
          <t-input style="width: 220px;" v-model="params.key" placeholder="输入产品名称、关键字、ID">
            <template #label>
              <icon-font name="search" size="16px" style="color:#000;font-weight: bold"/>
            </template>
          </t-input>
        </t-space>

        <div class="flex-4 flex align-center justify-end">
          <t-button theme="default" class="import-btn">导入</t-button>
        </div>
      </div>

      <div class="pt16">
        <t-table
          rowKey="id"
          :data="lists"
          :columns="columns"
          :stripe="stripe"
          :bordered="bordered"
          :hover="hover"
          :size="size"
          :max-height="472"
          :table-layout="tableLayout ? 'auto' : 'fixed'"
          :showHeader="showHeader"
          cellEmptyContent="-"
          resizable
          :loading="loading"
          @page-change="pageChange"
          :pagination="pagination"
          :selected-row-keys="selectedRowKeys"
          @select-change="handleSelectChange"
        >
          <template #thumb="{row}">
            <t-image-viewer :images="[row.thumb]" :closeOnEscKeydown="false">
              <template #trigger="{ open }">
                <div class="tdesign-demo-image-viewer__ui-image" @click="open">
                  <p class="prod-img">
                    <img alt="test" :src="row.thumb"/>
                  </p>
                  <div class="tdesign-demo-image-viewer__ui-image--hover">
                    <span><browse-icon size="1.4em"/> 预览</span>
                  </div>
                </div>
              </template>
            </t-image-viewer>
          </template>
          <template #day_rent="{row}">
            <p class="">￥{{ row.day_rent ? row.day_rent : '0.00' }}</p>
          </template>
          <template #selling_price="{row}">
            <p class="">￥{{ row.selling_price ? row.selling_price : '0.00' }}</p>
          </template>
          <template #cash_pledge="{row}">
            <p class="">￥{{ row.cash_pledge ? row.cash_pledge : '0.00' }}</p>
          </template>
          <template #stock_num="{row}">
            <p class="">{{ row.stock_num ? row.stock_num : '0' }}</p>
          </template>
          <template #status="{row}">
            <!-- -1 作废；0 下架；1 上架-->
            <t-tag v-if="row.status === 1" shape="round" variant="light" theme="success">上架</t-tag>
            <t-tag v-if="row.status === 0" shape="round" variant="light" theme="warning">下架</t-tag>
            <t-tag v-if="row.status === -1" shape="round" variant="light" theme="default">作废</t-tag>
            <!--<t-tag v-if="row.status === 'soldOut'" shape="round" variant="light" theme="danger">已售罄</t-tag>-->
          </template>
          <template #option="{row}">
            <t-space size="12px">
              <t-link theme="primary" class="opt-btn" variant="outline" @click="handleEdit(row)">编辑</t-link>
              <t-link theme="warning" class="opt-btn" variant="outline" @click="handleCopy(row)">复制</t-link>
              <t-popconfirm theme="danger" placement="top-right" confirm-btn="删除"
                            @confirm="handleDelete($event, row)">
                <template #content>
                  <div>
                    <h3 class="popconfirm-title">确认删除？</h3>
                    <p>删除后数据将进入回收站！是否确认删除？</p>
                  </div>
                </template>
                <t-link theme="danger" class="opt-btn" variant="outline">删除</t-link>
              </t-popconfirm>
            </t-space>
          </template>
        </t-table>
      </div>
    </t-card>
    <div v-if="selectedRowKeys?.length" class="footer">
      <t-space size="12px">
        <!--<t-popconfirm theme="danger" confirm-btn="删除" @confirm="handleDelete($event, row)">-->
        <!--  <template #content>-->
        <!--    <div>-->
        <!--      <h3 class="popconfirm-title">确认批量下架？</h3>-->
        <!--    </div>-->
        <!--  </template>-->
        <!--  <t-button theme="danger" class="action-btn2">批量下架</t-button>-->
        <!--</t-popconfirm>-->
        <t-button theme="primary" class="action-btn3" @click="handleBatchSettings">批量设置</t-button>
      </t-space>
      <t-space size="12px">
        <!--<p class="flex align-center">支出合计：<span class="income">¥829,823,00</span></p>-->
        <!--<p class="flex align-center">收入合计：<span class="expenditure">¥829,823,00</span></p>-->
        <!--<p>默认账户：<span>¥829,823,00</span></p>-->
        <!--<p>对公账户：<span>¥829,823,00</span></p>-->
      </t-space>
    </div>

    <product-add ref="add" @refresh="getData"></product-add>

    <!-- 批量设置 -->
    <batch-settings ref="batch" @refresh="getData"></batch-settings>
  </div>
</template>

<script lang="jsx">
import {IconFont, CalendarIcon, BrowseIcon} from "tdesign-icons-vue";
import IconUAV from "@/components/icon-UAV/index.vue";
import ProductAdd from "@/pages/product/list/add/index.vue";
import {productDeleteApi, productListApi} from "@/api/product";
import {userDeleteApi} from "@/api/user";
import {classifyListApi} from "@/api/classify";
import BatchSettings from "@/pages/product/list/components/batch-settings.vue";

export default {
  name: 'Book',
  components: {BatchSettings, IconUAV, IconFont, BrowseIcon, ProductAdd},
  data() {
    return {
      renderPrefixIcon: () => <CalendarIcon/>,
      params: {
        page: 1,  // 当前页
        limit: 10,  // 每页条数
        is_del: '', // 是否删除（1回收站；0正常）
        classify_id: '', // 分类
        brand_id: '', // 品牌
        hot_level: '',  // 推荐等级（5~1）
        key: '',  // 关键字
        status: 'all', // 状态
      },
      tabList: [
        {label: '全部', num: 564, value: 'all'},
        {label: '上架', num: 564, value: 'selling'},
        {label: '下架', num: 564, value: 'warehouse'},
        {label: '回收', num: 564, value: 'recycle'},
      ],  //
      lists: [],
      size: 'medium',
      tableLayout: false,
      stripe: false,
      bordered: false,
      hover: true,
      showHeader: true,
      columns: [
        {colKey: 'row-select', type: 'multiple', width: 50,},
        {colKey: 'id', title: 'ID', width: 70},
        {colKey: 'thumb', title: '产品图', align: 'center', width: 140,},
        {colKey: 'title', title: '名称', width: 240, ellipsis: true},
        {colKey: 'classify_title', title: '分类', width: 140, ellipsis: true},
        {colKey: 'brand_title', title: '品牌', width: 140, ellipsis: true},
        {colKey: 'day_rent', title: '日租价', width: 120},
        {colKey: 'selling_price', title: '售卖价', width: 120},
        {colKey: 'cash_pledge', title: '租赁押金', width: 140},
        {colKey: 'stock_num', title: '库存', width: 90},
        {colKey: 'status', title: '状态', width: 90},
        {colKey: 'sort', title: '排序', width: 100},
        {colKey: 'option', title: '操作', fixed: 'right', width: 140},
      ],
      selectedRowKeys: [],
      pagination: {
        defaultCurrent: 1,
        defaultPageSize: 5,
        total: 0,
      },
      loading: true,

      classifyOptions: [],
      brandOptions: [],
      levelOptions: [
        {label: '5级', value: 5},
        {label: '4级', value: 4},
        {label: '3级', value: 3},
        {label: '2级', value: 2},
        {label: '1级', value: 1},
      ],
    };
  },
  computed: {
    //...mapState('book', ['bookId'])
  },
  mounted() {
    this.getData()
    this.getOptions()
  },
  methods: {
    // 批量设置
    handleBatchSettings() {
      this.$refs.batch.open('batch', this.selectedRowKeys)
    },
    // 获取列表
    getOptions() {
      const params = {
        page: 1,  // 当前页
        limit: 9999,  // 每页条数
        // 模块类型（如产品分类 product_classify，说明 product_brand，发货说明 product_deliver，保障说明 product_safeguard ...）
      }
      classifyListApi({...params, belong_module: 'product_classify'}).then(res => {
        this.classifyOptions = res.data.map(item => ({label: item.title, value: item.id}))
      })
      classifyListApi({...params, belong_module: 'product_brand'}).then(res => {
        this.brandOptions = res.data.map(item => ({label: item.title, value: item.id}))
      })
    },
    // 添加
    goProductAdd() {
      this.$refs.add.open('add');
    },
    // 编辑
    handleEdit(row) {
      this.$refs.add.open('edit', row)
    },
    // 复制
    handleCopy(row) {
      this.$refs.add.open('copy', row)
    },
    // 删除
    handleDelete(evt, row) {
      productDeleteApi({id: row.id}).then(res => {
        this.$message.success(res.retmsg)
        this.getList()
      }).catch(err => {
        console.log('err', err)
      })
    },
    // 获取数据
    getData() {
      this.pagination.defaultCurrent = 1
      this.getList()
    },
    // 获取列表
    getList() {
      this.loading = true
      this.params.page = this.pagination.defaultCurrent
      productListApi(this.params).then(res => {
        this.pagination.total = res.count
        this.lists = res.data
      }).catch(err => {
      }).finally(() => {
        this.loading = false
      })
    },
    // 切换状态
    changeTab(tab) {
      this.params.user = tab.value
    },
    // 切换分页
    pageChange(pageInfo) {
      const {current, defaultCurrent, defaultPageSize,} = pageInfo
      this.pagination.defaultCurrent = current
      this.getList()
    },
    handleSelectChange(value, {selectedRowData}) {
      this.selectedRowKeys = value;
      console.log(value);
      console.log(selectedRowData);
    },
  },
}
</script>

<style lang="less" scoped>
.book-detail {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: var(--td-radius-medium);
  background-color: #FFFFFF;
}

/deep/ .content-card {

  .t-card__header {
    padding: var(--td-comp-paddingTB-xl) var(--td-comp-paddingLR-xl);
    padding-bottom: 16px;
  }

  .t-card__body {
    padding: var(--td-comp-paddingTB-s) var(--td-comp-paddingLR-xl) var(--td-comp-paddingTB-m);
  }

  .setting-btn {
    font-size: 14px;
    font-weight: 400;
    line-height: 19.6px;
    text-align: left;
    color: #005687;
  }

  .invite-btn {
    font-size: 12px;
    font-weight: 400;
    line-height: 16.8px;
    text-align: left;
    color: #005687;

    background-color: #F3FBFF;
    border: 1px solid #005687;
    border-radius: var(--td-radius-default);
  }
}

/deep/ .action-btn {
  width: 126px;
  height: 42px;
  font-size: 16px;
  font-weight: 600;
  line-height: 22.4px;
  text-align: left;
  color: #FFFFFF;

  .t-button__text {
    display: flex;
    align-items: center;
    justify-content: center;

    i {
      z-index: 2;
      font-size: 12px;
      font-weight: bold;
      margin-right: 6px;

      &:after {
        position: absolute;
        display: inline-block;
        content: '';
        top: 50%;
        left: 50%;
        width: 14px;
        height: 14px;
        z-index: -1;
        background-color: #FFFFFF;
        border-radius: var(--td-radius-round);
        transform: translateX(-50%) translateY(-50%);
      }
    }
  }
}

.action-btn1 {
  background-color: #14A54E;
  border: 1px solid #14A54E;

  i {
    color: #14A54E;
  }
}

.action-btn2 {
  background-color: #FF5C46;
  border: 1px solid #DFE3E7;
}

.action-btn3 {
  background-color: #565656;
  border: 1px solid #DFE3E7;
}

.action-btn4 {
  color: black;
  background-color: #F9F9F9;
  border: 1px solid #E1E1E1;
}

.members {
  display: flex;
  gap: 12px;

  .member-list {
    display: flex;
    align-items: center;
    gap: 12px;

  }

  .member-item {
    //width: 100px;
    flex-shrink: 0;
    padding: 6px 12px;
    padding-right: 33px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;

    .body-wrap {
      position: relative;
      padding-bottom: 20px;

      .num {
        position: absolute;
        top: 0;
        right: 0;
        font-size: 12px;
        font-weight: 700;
        line-height: 13.97px;
        text-align: left;
        color: #00000099;
        transform: translateX(100%);
      }
    }

    .body {
      opacity: 0.4;
      display: flex;
      align-items: center;

    }

    .avatar {
      width: 30px;
      height: 30px;
      overflow: hidden;
      margin-right: 6px;
      border-radius: var(--td-radius-round);

      img {
        width: 100%;
        height: 100%;
      }
    }

    h3 {
      font-size: 18px;
      font-weight: 400;
      line-height: 25.2px;
      text-align: left;
      color: #000000;
    }
  }

  .active-member {
    position: relative;

    .body {
      opacity: 1;
    }

    h3 {
      font-weight: 600;
    }

    &:after {
      position: absolute;
      content: '';
      left: 50%;
      right: 0;
      bottom: 0;
      width: 140%;
      height: 6px;
      transform: translateX(-50%) translateY(-100%);
      background-color: #FF5C46;
      border-radius: var(--td-radius-round);
    }
  }
}

/deep/ .filter {
  padding-top: 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .t-input, .t-input.t-is-readonly {
    background-color: #F8F8F8;
  }
}

//.import-btn {
//  width: 69px;
//  color: #000000;
//  font-size: 14px;
//  font-weight: 400;
//  line-height: 19.6px;
//  background-color: #F9F9F9;
//  border: 1px solid #E1E1E1;
//}

.income {
  color: #14A54E;
  display: flex;
  align-items: center;

  i {
    width: 17px;
    height: 17px;
    color: #FFFFFF;
    font-size: 12px;
    display: flex;
    margin-left: 8px;
    align-items: center;
    justify-content: center;
    background-color: #14A54E;
    border-radius: var(--td-radius-small);
  }
}

.expenditure {
  color: #FF5C46;
  display: flex;
  align-items: center;

  i {
    width: 17px;
    height: 17px;
    color: #FFFFFF;
    font-size: 12px;
    display: flex;
    margin-left: 8px;
    align-items: center;
    justify-content: center;
    background-color: #FF5C46;
    border-radius: var(--td-radius-small);
  }
}

.footer {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #F9F9F9;
  padding: var(--td-comp-paddingTB-m) var(--td-comp-paddingLR-xl);
  padding-right: var(--td-comp-paddingLR-xxl);
}
</style>
